<template>
  <div>
    <Form ref="form"  :model="form">
      <FormItem prop="kind">
        <Input placeholder="输入关键字进行过滤" v-model="filterText" />
      </FormItem>
    </Form>
    <Tree :data="data"  
    :check-strictly="true"
    :check-directly="true"
    @on-select-change="treeChange">
    </Tree>
    <Row class="code-row-bg" justify="end" type="flex">
      <Button @click="cancel" style="margin-right: 10px">取消</Button>
      <Button @click="save" type="primary">保存</Button>
    </Row>
  </div>
</template>
<script>
import { labelApi } from "@/api/label";
import { assetsApi } from "@/api/assets";
export default {
  name: "CodeReviewListForm",
  components: {},
  props: {
    //是否为更新表单
    isUpdate: {
      type: Boolean,
      default: true,
    },
    //更新的表单数据对象
    updateData: {
      type: Array,
    },
  },
  data() {
    return {
      filterText: null,
      data:[],
      selectNode:null,
      assetIdList:[]
    };
  },
  watch: {
    filterText(val) {
      let arr = JSON.parse(JSON.stringify(this.data));
      this.data = this.filterNode(val, arr);
    },
    updateData(newData,oldData){
        if(newData.length){
            let arr = []
            newData.forEach(item=>{
                arr.push(item.id)
            })
            this.assetIdList= arr
        }
    }
  },
  mounted() {
    //this.data = this.data2;
    //查询标签类型
    this.queryList();
  },
  created() {},
  methods: {
    async queryList(){
        let result = await labelApi.typeList({});
        this.data = result.data;
    },
    cancel() {
      this.$emit("on-form-close");
    },
    async save() {
        //判断类型是否选择
        if(!this.selectNode){
            this.$Message.error("请选择类型");
        }
        this.$Spin.show();
        let param = {
          "assetIdList":this.assetIdList,
          "categoryId": this.selectNode.id
        }
        let res = await assetsApi.mulUpdateType(param);
        this.$Message.success("更新成功");
        this.$Spin.hide();
        this.$emit("on-form-close");
    },
    //过滤树节点
    filterNode(val, List) {
      //过滤出满足条件的数组
      let List1 = List.filter((item) => {
        //如果该元素有children，则优先处理children的
        if (item.children) {
          //过滤children里面满足条件的
          item.children = this.filterNode(val, item.children);
          //如果children里面没有满足条件的，则进入，否则返回true
          if (!item.children.some((item1) => item1.title.indexOf(val) !== -1)) {
            //判断children里面是否还有一层children,如果有则对内层children继续调用函数判断，否则返回title中含有关键字的元素
            if (item.children.some((item1) => item1.children)) {
              item.children.forEach((item2) => {
                item2.children && this.filterNode(val, item2.children);
              });
            } else {
              return item.title.indexOf(val) > -1;
            }
          }
          return true;
        }
        //返回title中含有关键字的元素
        return item.title.indexOf(val) > -1;
      });
      return List1;
    },
         
    treeChange(arr, obj) {
       this.selectNode = arr[0];
       console.log(arr, obj, 1234567);
     }
    
  },
};
</script>